package top.leinx.tmind.component;

/**
 * 项目名称: TaoMind
 * 创建时间: 2021/4/16 15:04
 * 作者姓名: xiaolei
 */

public final class MindSubjects {
    public static void adjustPosition(MindSubject subject, int position, double cellHeight) {
        double totalHeight = subject.getNumberOfLeaf(position) * (cellHeight + Config.V_GAP) - Config.V_GAP;
        double sumHeight = 0;
        for (int i = 0; i < subject.getItems().size(); i++) {
            MindSubject ms = subject.getItems().get(i);
            if (ms.getPostion() == position) {
                // 设置ms的X坐标
                if (ms.getPostion() == Config.LAYOUT_RIGHT) {
//                    ms.setX(ms.getParent().getX() + ms.getParent().getLabel().getWidth() + Config.H_GAP);
                    ms.getLabel().layoutXProperty().bind(
                            ms.getParent().getLabel().layoutXProperty().add(
                                    ms.getParent().getLabel().widthProperty().add(
                                            Config.H_GAP
                                    )
                            )
                    );
                } else {
                    ms.setX(ms.getParent().getX() - ms.getLabel().getWidth() - Config.H_GAP);
                    ms.getLabel().layoutXProperty().bind(
                            ms.getParent().getLabel().layoutXProperty().subtract(
                                    Config.H_GAP
                            ).subtract(
                                    ms.getLabel().widthProperty())
                    );
                }

                // 当前结点的总高度
                double msTotalHeight = ms.getNumberOfLeaf(position) * (cellHeight + Config.V_GAP) - Config.V_GAP;
                double top = 0;
                if (i == 0) {
                    top = ms.getParent().getY() - totalHeight / 2 + cellHeight / 2;
                    sumHeight = top;
                } else {
                    top = sumHeight;
                }
                ms.setY(top + msTotalHeight / 2 - cellHeight / 2);
                sumHeight += msTotalHeight + Config.V_GAP;
            }

            adjustPosition(ms, position, cellHeight);
        }

    }
}
